package mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import tech.michaelx.loadinglibrary.LoadingLayout;

/**
 * Created by ${cbf} on 2017/12/2.
 */

public class BaseRefreshActivity extends BaseActivity {
    @BindView(R.id.rv_base_recyclerview)
    RecyclerView rvBaseRecyclerview;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    @BindView(R.id.loadinglayout)
    LoadingLayout loadinglayout;
    protected BaseAdapter adapter;
    private List<Object> objects = new ArrayList<>();

    @Override
    protected int setContentLayout() {
        return R.layout.base_refresh_loading;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void init() {
        rvBaseRecyclerview.setLayoutManager(new LinearLayoutManager(BaseRefreshActivity.this));
        for (int i = 0; i < 50; i++) {
            objects.add("A" + i);
        }
        adapter = new BaseAdapter(BaseRefreshActivity.this) {

            private TextView tvs1;

            @Override
            protected int layoutadapter() {
                return R.layout.item_test_activity;
            }

            @Override
            protected void layoutData(List<Object> object, int position) {
                String s = (String) objects.get(position);
                tvs1.setText(s);
            }

            @Override
            protected void layoutFindViewById(View itemView) {
                tvs1 = (TextView) itemView.findViewById(R.id.tv_test1);
            }
        };
        rvBaseRecyclerview.setAdapter(adapter);

    }

    @Override
    protected void setListeners() {
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                adapter.setData(objects);
                refreshlayout.finishRefresh(500);
                loadinglayout.loadComplete();
            }
        });
        smartrefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                adapter.addData(objects);
                refreshlayout.finishLoadmore(500);
                loadinglayout.loadComplete();
            }
        });

    }

    @Override
    public void onClick(View view) {

    }
}
